package com.fhzn.auth.web.converter;

import com.fhzn.auth.entity.Role;
import com.fhzn.auth.tools.DateTimeUtils;
import com.fhzn.auth.web.vo.RoleVO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.compress.utils.Lists;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 老顾
 * @apiNote
 * @since 2023/12/25
 */
public interface RoleConverter {

    /**
     * 角色实体转换
     *
     * @param role 请求参数
     * @return 返回参数
     */
    static RoleVO fromRole(Role role) {
        RoleVO roleVO = new RoleVO();
        if (role != null) {
            roleVO.setStatus(role.getStatus());
            roleVO.setId(role.getId());
            roleVO.setAppCode(role.getAppCode());
            roleVO.setCode(role.getCode());
            roleVO.setComment(role.getComment());
            roleVO.setName(role.getName());
            roleVO.setSign(role.getSign());
            roleVO.setModifier(role.getModifier());
            if (role.getUpdatedTime() != null) {
                roleVO.setUpdatedTime(DateTimeUtils.format(role.getUpdatedTime()));
            }
            if (role.getCreatedTime() != null) {
                roleVO.setCreatedTime(DateTimeUtils.format(role.getCreatedTime()));
            }
            roleVO.setGroupId(role.getGroupId());
            roleVO.setCountOfAssignedUser(role.getCountOfAssignedUser());
            return roleVO;
        }
        return roleVO;
    }

    /**
     * 角色实体转换
     *
     * @param roles 请求参数
     * @return 返回参数
     */
    static List<RoleVO> convertToVoList(List<Role> roles) {
        if (CollectionUtils.isEmpty(roles)) {
            return Lists.newArrayList();
        }
        return roles.stream().map(RoleConverter::fromRole).collect(Collectors.toList());
    }
}
